package dcds2;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mesan
 */
public class AddTransactionWindow extends javax.swing.JFrame {

    Connection conn;
    ResultSet rs;
    Statement stat;
    String[] listPatients;
    String[] procedures;
    Double[] procedurePrices;
    String fName;
    String mName;
    String lName;
    String pName;
    int transactionID;
    int procedureNo = 1;
    int tableRows = 0;
    DefaultTableModel modelProceduresList = new DefaultTableModel(new Object[][]{}, new String[] {"Procedure","Dentist","Price"});
   
    public String[] months = {"January","February", "March","April", "May", "June", "July", "August", "September"
                        , "October", "November", "December"};
     public String[] days31 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
    "18", "19", "20", "21", "22", "23", "24", "25", "26","27","28","29", "30", "31"};
    
     public String[] days30 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
    "18", "19", "20", "21", "22", "23", "24", "25", "26","27","28","29", "30"};
     public String[] days28 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
    "18", "19", "20", "21", "22", "23", "24", "25", "26","27","28"};
     public String[] days29 = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", 
    "18", "19", "20", "21", "22", "23", "24", "25", "26","27","28","29"};
    
    
    /** Creates new form AddTransactionWindow */
    public AddTransactionWindow() {
        
        setResizable(false);

        //for Patient Name combobox
        try {
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i = 0;

            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM dcds.patient ORDER BY pLastName ASC");

            ResultSet rs = pstmt.executeQuery();
            rs.last();
            listPatients = new String[rs.getRow()];
            rs.beforeFirst();

            while (rs.next()) {

                fName = rs.getString("pFirstName");
                mName = rs.getString("pMiddleName");
                lName = rs.getString("pLastName");
                pName = lName + ", " + fName + ", " + mName;
                listPatients[i] = pName;

                i++;
            }
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //for Procedures combobox
        try {
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i = 0;

            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM dcds.procedures");

            ResultSet rs = pstmt.executeQuery();
            rs.last();
            procedures = new String[rs.getRow()];
            procedurePrices = new Double[rs.getRow()];
            rs.beforeFirst();

            while (rs.next()) {
                procedures[i] = rs.getString("procedureName");
                procedurePrices[i] = rs.getDouble("procedurePrice");

                i++;
            }
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        initComponents();
        tblProceduresList.setAutoCreateRowSorter(true);
        txtPrice.setText(procedurePrices[comboProcedure.getSelectedIndex()].toString());
        setLocationRelativeTo(null);
        

    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        comboMonth = new javax.swing.JComboBox();
        comboDay = new javax.swing.JComboBox();
        txtYear = new javax.swing.JTextField();
        btnAddProcedure = new javax.swing.JButton();
        comboName = new javax.swing.JComboBox();
        comboProcedure = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtPrice = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblProceduresList = new javax.swing.JTable();
        btnCancel = new javax.swing.JButton();
        btnConfirm = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        comboDentist = new javax.swing.JComboBox();
        btnUndo = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("DCDS - Add Transaction");
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));

        jLabel1.setFont(new java.awt.Font("Century", 0, 11));
        jLabel1.setText("Date:");

        comboMonth.setFont(new java.awt.Font("Century", 0, 11));
        comboMonth.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }));
        comboMonth.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboMonthActionPerformed(evt);
            }
        });

        comboDay.setFont(new java.awt.Font("Century", 0, 11));
        comboDay.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31" }));
        comboDay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboDayActionPerformed(evt);
            }
        });

        txtYear.setFont(new java.awt.Font("Century", 0, 11)); // NOI18N
        txtYear.setText("<year>");
        txtYear.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtYearMouseClicked(evt);
            }
        });
        txtYear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtYearActionPerformed(evt);
            }
        });
        txtYear.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                txtYearFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtYearFocusLost(evt);
            }
        });

        btnAddProcedure.setFont(new java.awt.Font("Century", 0, 11));
        btnAddProcedure.setText("Add Procedure");
        btnAddProcedure.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddProcedureActionPerformed(evt);
            }
        });

        comboName.setFont(new java.awt.Font("Century", 0, 11));
        comboName.setModel(new javax.swing.DefaultComboBoxModel(listPatients));
        comboName.setAutoscrolls(true);

        comboProcedure.setFont(new java.awt.Font("Century", 0, 11));
        comboProcedure.setModel(new javax.swing.DefaultComboBoxModel(procedures));
        comboProcedure.setSelectedIndex(0);
        comboProcedure.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboProcedureActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Century", 0, 11));
        jLabel2.setText("Name:");

        jLabel3.setFont(new java.awt.Font("Century", 0, 11));
        jLabel3.setText("Procedure:");

        jLabel4.setFont(new java.awt.Font("Century", 0, 11));
        jLabel4.setText("Dentist:");

        txtPrice.setFont(new java.awt.Font("Century", 0, 11));

        tblProceduresList.setFont(new java.awt.Font("Century", 0, 11));
        tblProceduresList.setModel(modelProceduresList);
        jScrollPane1.setViewportView(tblProceduresList);

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/module/ButtonCancel.png"))); // NOI18N
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnConfirm.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/module/ButtonConfirm.png"))); // NOI18N
        btnConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnConfirmActionPerformed(evt);
            }
        });

        jLabel5.setFont(new java.awt.Font("Century", 0, 11));
        jLabel5.setText("Price:");

        comboDentist.setFont(new java.awt.Font("Century", 0, 11));
        comboDentist.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Dr. Rex T. Mayor", "Dr. Rowena B. Mayor" }));

        btnUndo.setFont(new java.awt.Font("Century", 0, 11));
        btnUndo.setText("Undo");
        btnUndo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUndoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnUndo)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(50, 50, 50))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(comboDentist, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnAddProcedure)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(comboProcedure, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(txtYear, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(comboMonth, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addComponent(comboName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 171, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(comboDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(209, Short.MAX_VALUE)
                .addComponent(btnConfirm, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtYear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(comboMonth, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(comboDay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(comboName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(comboDentist, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboProcedure, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel5)
                    .addComponent(txtPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAddProcedure)
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnUndo)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnConfirm)
                    .addComponent(btnCancel))
                .addContainerGap(18, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtYearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtYearActionPerformed
         int year = 0;
        if(txtYear.getText().compareTo("<year>")==0){
            JFrame frame = new JFrame();
            JOptionPane.showMessageDialog(frame, "Please input year first", "NOTICE!", JOptionPane.PLAIN_MESSAGE);
        }else year = Integer.parseInt(txtYear.getText());
        if(year%4==0 && (year%100!=0 || year%400==0) )
            comboDay.setModel(new DefaultComboBoxModel(days29));
       else comboDay.setModel(new DefaultComboBoxModel(days28));
}//GEN-LAST:event_txtYearActionPerformed

    private void btnAddProcedureActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddProcedureActionPerformed
        try {
            Double procedurePrice = Double.parseDouble(txtPrice.getText());
            try {
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();

            int i = 0;
            PreparedStatement pstmt1 = conn.prepareStatement("SELECT transactionID FROM transactions");
            
            ResultSet rs1 = pstmt1.executeQuery();
            rs1.last();
            
            transactionID = rs1.getRow() + 1;
            
            String procedureName = comboProcedure.getSelectedItem().toString();
            String dentistName = comboDentist.getSelectedItem().toString();
            
            PreparedStatement pstmt2 = conn.prepareStatement("INSERT INTO transprocdetails (transactionID, procedureNo, procedureName, dentistName, procedurePrice) VALUES ('"+transactionID+"','"+procedureNo+"','"+procedureName+"','"+dentistName+"','"+procedurePrice+"')");
            
            pstmt2.executeUpdate();
            procedureNo++;
            conn.close();

            modelProceduresList.addRow(new Object[] {"","","",});
            if(modelProceduresList.getRowCount() == 0) {
                tblProceduresList.setValueAt(procedureName, modelProceduresList.getRowCount(), 0);
                tblProceduresList.setValueAt(dentistName, modelProceduresList.getRowCount(), 1);
                tblProceduresList.setValueAt(procedurePrice, modelProceduresList.getRowCount(), 2);
            }else{
                tblProceduresList.setValueAt(procedureName, modelProceduresList.getRowCount()-1, 0);
                tblProceduresList.setValueAt(dentistName, modelProceduresList.getRowCount()-1, 1);
                tblProceduresList.setValueAt(procedurePrice, modelProceduresList.getRowCount()-1, 2);
            }
            
            tableRows++;
            } catch (Exception e) {
            e.printStackTrace();
            } 
        }catch (NumberFormatException nfe){
            JOptionPane.showMessageDialog(new JFrame(), "Please enter a valid price!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
        }
}//GEN-LAST:event_btnAddProcedureActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        try {
            ConnectionFactory myFactory = ConnectionFactory.getFactory();
            Connection conn = myFactory.getConnection();
            
            PreparedStatement pstmt = conn.prepareStatement("DELETE FROM transprocdetails WHERE transactionID = '"+transactionID+"'");
            pstmt.executeUpdate();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        dispose();
        new MainWindow(3).setVisible(true);
}//GEN-LAST:event_btnCancelActionPerformed

    private void btnConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConfirmActionPerformed
        JFrame frame = new JFrame();
        String Month = comboMonth.getSelectedItem().toString();
        String Day = comboDay.getSelectedItem().toString();
        String Year = txtYear.getText();
        int yearValid = 0;
        
        try{
            int nYear = Integer.parseInt(Year);
            int selection = JOptionPane.showConfirmDialog(frame, "Are all the fields correct?", "Confirmation!", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (selection == JOptionPane.YES_OPTION) {
                if(Year.length() == 4 && nYear >= 2005 ){
                    try {
                        yearValid = 1;
                        ConnectionFactory myFactory = ConnectionFactory.getFactory();
                        Connection conn = myFactory.getConnection();

                        pName = comboName.getSelectedItem().toString();
                        StringTokenizer str = new StringTokenizer(pName, ",");

                        lName = str.nextToken();
                        fName = str.nextToken().trim();
                        mName = str.nextToken().trim();

                        PreparedStatement pstmt0 = conn.prepareStatement("SELECT patientID, pLastName, pFirstName, pMiddleName FROM patient WHERE (pLastName = '"+lName+"' AND pFirstName = '"+fName+"' AND pMiddleName = '"+mName+"')");
                        ResultSet rs0 = pstmt0.executeQuery();
                        rs0.next();
                        int patientID = rs0.getInt("patientID");

                        String transDate = Month + " " + Day + ", " + Year; 
                        PreparedStatement pstmt1 = conn.prepareStatement("INSERT INTO transactions (transactionID,patientID,pName,transactionDate) VALUES ('"+transactionID+"','"+patientID+"','"+pName+"','"+transDate+"')");

                        pstmt1.executeUpdate();

                        conn.close();

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                else {
                    JOptionPane.showMessageDialog(frame, "Year of transaction cannot be before 2005!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
                }
            
            }
        }
        catch (NumberFormatException nfe){
            JOptionPane.showMessageDialog(new JFrame(), "Please enter a valid year!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
        }
        if(yearValid == 1) {
            dispose();
            new MainWindow(3).setVisible(true);
        }
    }//GEN-LAST:event_btnConfirmActionPerformed

    private void btnUndoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUndoActionPerformed

        if(modelProceduresList.getRowCount() != 0) {
            try {
                ConnectionFactory myFactory = ConnectionFactory.getFactory();
                Connection conn = myFactory.getConnection();
                procedureNo--;
                PreparedStatement pstmt = conn.prepareStatement("DELETE FROM transprocdetails WHERE procedureNo = '"+procedureNo+"' AND transactionID = '"+transactionID+"'");
                pstmt.executeUpdate();
                conn.close();

                tblProceduresList.setValueAt("", modelProceduresList.getRowCount()-1, 0);
                tblProceduresList.setValueAt("", modelProceduresList.getRowCount()-1, 1);
                tblProceduresList.setValueAt("", modelProceduresList.getRowCount()-1, 2);
                modelProceduresList.removeRow(modelProceduresList.getRowCount()-1);

            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        else
            JOptionPane.showMessageDialog(new JFrame(), "There are no more procedure adds to undo!","NOTICE!",JOptionPane.PLAIN_MESSAGE);
    }//GEN-LAST:event_btnUndoActionPerformed

    private void comboProcedureActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboProcedureActionPerformed
        txtPrice.setText(procedurePrices[comboProcedure.getSelectedIndex()].toString());
    }//GEN-LAST:event_comboProcedureActionPerformed

private void comboMonthActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboMonthActionPerformed
        int index = comboMonth.getSelectedIndex()+1;
    if(index == 2){
        int year = 0;
        if(txtYear.getText().compareTo("<year>")==0){
            JFrame frame = new JFrame();
            JOptionPane.showMessageDialog(frame, "Please input year first", "NOTICE!", JOptionPane.PLAIN_MESSAGE);
}       else year = Integer.parseInt(txtYear.getText());
        if(year%4==0 && (year%100!=0 || year%400==0) )
            comboDay.setModel(new DefaultComboBoxModel(days29));
       else comboDay.setModel(new DefaultComboBoxModel(days28));
        
    } else if(index == 1 || index == 3 || index == 5 || index == 7 || index == 8 || index == 10 ||index == 12){
        comboDay.setModel(new DefaultComboBoxModel(days31));
    } else 
        comboDay.setModel(new DefaultComboBoxModel(days30));
    
}//GEN-LAST:event_comboMonthActionPerformed

private void comboDayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboDayActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_comboDayActionPerformed

private void txtYearMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtYearMouseClicked
    txtYear.setText("");
}//GEN-LAST:event_txtYearMouseClicked

private void txtYearFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtYearFocusLost
    if (txtYear.getText().equals("")) {    
        txtYear.setText("<year>");
    }
}//GEN-LAST:event_txtYearFocusLost

private void txtYearFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtYearFocusGained
    if (txtYear.getText().equals(("<year>"))) {
        txtYear.setText("");
    }
}//GEN-LAST:event_txtYearFocusGained

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AddTransactionWindow().setVisible(true);
            }
        });
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddProcedure;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnConfirm;
    private javax.swing.JButton btnUndo;
    private javax.swing.JComboBox comboDay;
    private javax.swing.JComboBox comboDentist;
    private javax.swing.JComboBox comboMonth;
    private javax.swing.JComboBox comboName;
    private javax.swing.JComboBox comboProcedure;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable tblProceduresList;
    private javax.swing.JTextField txtPrice;
    private javax.swing.JTextField txtYear;
    // End of variables declaration//GEN-END:variables
}
